#include <stdio.h>
int main()
{
    int a,b,c;
    while (scanf("%d%d",&a,&b)!=EOF)
    {
        if (a==0&&b==0)break;
        c=a%1000;
        a=c;
        b--;
        while (b--)
        {
            c=(c*a)%1000;
        }
        printf("%d\n",c);
    }
    return 0;
}
